// pages/coupon/coupon.js
var Zan = require('../../../dist/index.js');
var coupon = require('../../../template/MiaoDaoTemp.js');
var Zan = require('../../../dist/index');
let network = null;
let utils = null;
let api = null;
const app = getApp();
let start01 = 0;
let start02 = 0;
let start03 = 0;

Page(Object.assign({}, Zan.Tab, Zan.TopTips, {

  /**
   * 页面的初始数据
   */
  data: {
    tab: {
      list: [{
        id: 0,
        title: '未使用'
      }, {
        id: 1,
        title: '已使用'
      }, {
        id: 2,
        title: '已过期'
      }],
      selectedId: 0,
      height: 45,
      scroll: false
    },
    activeIndex: 0,
    recordsTotal01: 0,
    recordsTotal02: 0,
    recordsTotal03: 0,
    coupon01: [],
    coupon02: [],
    coupon03: [],
    weekcon:[],
    nodata: {
      msg: '没有优惠券',
      notype: 2,
      imgurl: '../../../images/nodatas.png',
      nourl: ''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    network = new app.NetWorkUtils();
    utils = new app.Utils();
    api = new app.Api();

    this.onPullDownRefresh();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    if (this.data.activeIndex == 0) {
      this.setData({
        coupon01: [],
      });
    } else if (this.data.activeIndex == 1) {
      this.setData({
        coupon02: [],
      });
    } else if (this.data.activeIndex == 2) {
      this.setData({
        coupon03: [],
      });
    }
    network._post_json(this, api.getMyCoupon, { status: this.data.activeIndex, storeId: app.globalData.store.id}, success);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // if (this.data.activeIndex == 0) {
    //   if (this.data.coupon01.length < this.data.recordsTotal01) {
    //     start01 = start01 + 20;
    //     network._post_page_json(this, api.getMyCoupon, start01, { status: this.data.activeIndex }, success01);
    //   }
    // } else if (this.data.activeIndex == 1) {
    //   if (this.data.coupon02.length < this.data.recordsTotal02) {
    //     start02 = start02 + 20;
    //     network._post_page_json(this, api.getMyCoupon, start02, { status: this.data.activeIndex }, success02);
    //   }
    // } else if (this.data.activeIndex == 2) {
    //   if (this.data.coupon03.length < this.data.recordsTotal03) {
    //     start03 = start03 + 20;
    //     network._post_page_json(this, api.getMyCoupon, start03, { status: this.data.activeIndex }, success03);
    //   }
    // }
  
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // },
  handleZanTabChange(e) {
    var componentId = e.componentId;
    var selectedId = e.selectedId;

    this.setData({
      [`${componentId}.selectedId`]: selectedId
    });


    this.setData({
      activeIndex: selectedId
    })
    if (this.data.tab.selectedId == 0) {
      this.setData({
        coupon01: [],
      });
    } else if (this.data.tab.selectedId == 1) {
      this.setData({
        coupon02: [],
      });
    } else if (this.data.tab.selectedId == 2) {
      this.setData({
        coupon03: [],
      });
    }
    network._post_json(this, api.getMyCoupon, { status: this.data.tab.selectedId, storeId: app.globalData.store.id }, success);
  },

  /**
   * 点击tab
   */
  selected:function(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      activeIndex: index
    })    
  },

  // 查看优惠券详情
  couponDetails: function (e) {
    coupon.couponDetails(this, e);
  },

  // 立即使用优惠券
  userCoupon: function (e) {
    //coupon.userCoupon(this, e)
    var coupon = e.currentTarget.dataset.item;
    wx.switchTab({
      url: '/pages/product/Product?coupon=' + JSON.stringify(coupon)
    })
  },

  refresh() {
    this.setData({
      coupon01: [],
    });
    network._post_json(this, api.getMyCoupon, { status: this.data.activeIndex, storeId: app.globalData.store.id }, success);
  }
}))

// 获取未使用优惠列表结果
var success = function (that, res) {
  if (res.data.data) {
    var coupon = res.data.data;
    
    for (var i = 0; i < coupon.length; i++) {
      coupon[i].startTime = utils.formatDate02(new Date(coupon[i].startTime * 1000))
      coupon[i].endTime = utils.formatDate02(new Date(coupon[i].endTime * 1000))

      if (coupon[i].useWeek != "8"){
        let useWeek = coupon[i].useWeek.split(",");
        let week = ["", "周一", "周二", "周三", "周四", "周五", "周六", "周天"];
        let weekcon = [];
        for (var n = 0; n < useWeek.length; n++) {
          let text = week[useWeek[n]];
          weekcon.push(text);
          that.setData({
            weekcon: weekcon
          })
        }
        coupon[i].weekcon = that.data.weekcon;
      }
    }
    if (that.data.activeIndex == 0) {
      that.setData({
        coupon01: that.data.coupon01.concat(coupon),
      });
    } else if (that.data.activeIndex == 1) {
      that.setData({
        coupon02: that.data.coupon02.concat(coupon),
      });
    } else if (that.data.activeIndex == 2) {
      that.setData({
        coupon03: that.data.coupon03.concat(coupon),
      });
    }

  }
}